package com.suieqian.controller;

import com.suieqian.service.impl.FastDFSClientImpl;
import com.suieqian.util.Message;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * fastdfs
 *
 * @author gyf
 */
@RestController
@RequestMapping("/fastdfs")
@Slf4j
public class FastDFSController {
    @Autowired
    FastDFSClientImpl fFastDFSClientImpl;

    @ApiOperation("上传标准图片")
    @PostMapping("/uploadFile")
    public Message<String> uploadExcel(@RequestPart("file") MultipartFile file) {
        try {
            String url = fFastDFSClientImpl.uploadFile(file);
            return Message.success(url);
        } catch (IOException e) {
            log.error("上传失败", e);
            return Message.error("上传失败");
        }

    }

    @RequestMapping(value = "/uplaod", method = RequestMethod.GET)
    public void upload() {
        File file = new File("d://hellofastdfs.txt");
        String url = fFastDFSClientImpl.upload(file, "txt");
        log.info(url);
    }

    @RequestMapping(value = "/del", method = RequestMethod.GET)
    public void del() {
        String id = "group1/M00/00/00/ezg7iGNOWxeADbKYAAIvelllwxU289.txt";
        fFastDFSClientImpl.deleteFile(id);
    }
}
